const request = require('../../utils/https.js')
var userId, height, fromUser;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indexId: 0,
    pageNum: 1,
    loadingPrompt: false,
    notPrompt: false,
    categoryArray:[],
    categoryProdArray:[],
    categoryIndex:0,
    brandName:"品牌",
    cateName:"品类",
    brandShow:false,
    cateShow:false,
    searchLen:0,
    iswitch:1,
    nowSwitch:-1,
    finish:false,
    searchWidth:620
  },
  // 左侧点击事件
  jumpIndex(e) {

    // this.animate('.goods_box', [{
    //     opacity: 1
    //   },
    //   {
    //     opacity: 0.8
    //   },
    //   {
    //     opacity: 0.5
    //   },
    //   {
    //     opacity: 0.3
    //   },
    //   {
    //     opacity: 0
    //   },

    // ], 100, function () {
    //   this.clearAnimation('.goods_box', {
    //     opacity: true
    //   }, function () {
    //     console.log("清除了#container上的动画属性")
    //   })
    // }.bind(this))

    const that = this;
    let index = e.currentTarget.dataset.menuindex;
    let navList = that.data.navList;

    that.setData({
      indexId: index,
      pageNum: 1,
      notPrompt: false,
      loadingPrompt: false,
      goods_logo: false
    });
    this.setData({
      goodsList: []
    });
    request._get(getApp().Api_url + 'goods/listByCateId', { //商品列表接口
      cateId: navList[index].cateId,
      userId: userId,
      pageNum: 1,
      pageSize: 6
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        var goodsList = res.data.data;
        var list_goods_img = res.data.oss[0];
        if (list_goods_img) {
          that.setData({
            goods_logo: true
          })
        } else {
          that.setData({
            goods_logo: false
          })
        }
        that.setData({
          goodsList: goodsList,
          list_goods_img: list_goods_img,
          cateId: navList[index].cateId
        })
      //   that.animate('.goods_box', [{
      //     opacity: 0
      //   },
      //   {
      //     opacity: 0.3
      //   },
      //   {
      //     opacity: 0.5
      //   },
      //   {
      //     opacity: 0.8
      //   },
      //   {
      //     opacity: 1
      //   },
  
      // ], 200, function () {
      //   this.clearAnimation('.goods_box', {
      //     opacity: true
      //   }, function () {
      //     console.log("清除了#container上的动画属性")
      //   })
      // }.bind(that))
      }
    }, function () {})
  },

  // 左侧点击事件
  findCateItemById(e) {

    this.animate('.goods_box', [{
      opacity: 0.0
    },
      {
        opacity: 0.3
      },
      {
        opacity: 0.5
      },
      {
        opacity: 0.8
      },
      {
        opacity: 1.0
      },

    ], 500, function () {
      this.clearAnimation('.goods_box', {
        opacity: true
      }, function () {
        console.log("清除了#container上的动画属性")
      })
    }.bind(this))

    const that = this;
    that.setData({
      cateItemList:null
    });
    let index = e.currentTarget.dataset.menuindex;
    let categoryArray = that.data.categoryArray;
    that.setData({
      categoryIndex: index,
      notPrompt: false,
      loadingPrompt: false
    });

    console.log("当前点击是categoryArray[index].cateId,:",categoryArray[index].cateId);
    request._get(getApp().Api_url + 'brand/cateInfo', { //商品列表接口
      cateId: categoryArray[index].cateId,
      userId: userId
    }, function (res) {
      if (res.data.code == 200) {
        let cateDataArray = res.data.data;
        if(cateDataArray!=null){
          that.setData({
            cateItemList:cateDataArray[0].childs,
            categoryProdList:cateDataArray[0].prods
          });
        }
      }
    }, function () {})
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    userId = wx.getStorageSync('userId');
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight
        });
      }
    });
    that.brandSwitch();
  },
  onSwitch(e){
    const that=this;
    let iswitch=e.currentTarget.dataset.switch;
    console.log('iswitch===>',iswitch)
    that.setData({
      iswitch:iswitch
    })

    if(iswitch==2){
        //商品列表
      that.pullCateData();
    }else if(iswitch==1){
      that.pullBrandData();
    }
  },

  jumpToProdOfCateId(e){
    let cid = e.currentTarget.dataset.cid;
    let cname = e.currentTarget.dataset.cname;
    wx.navigateTo({
      url: '/packageB/pages/prodCate/prodCate?cid='+cid+"&cname="+cname,
    })
  },
  onFocus() {
    wx.navigateTo({
      url: '/packageA/pages/Search/Search',
    })
  },
  //查看详情页
  goOrder(e) {
    const that = this;

    let userId = wx.getStorageSync("userId");
    if (userId == '' || userId == null || userId == 'undefined') {
      // wx.reLaunch({
      //   url: '/pages/login/login?path=/pages/list/list'
      // })
      // return;
      userId = null;
    }
    let goodsId = e.currentTarget.dataset.goodsid;
    let goodsType = e.currentTarget.dataset.kinds;
    let fast = e.currentTarget.dataset.fast;

    wx.navigateTo({
      url: '/pages/gift_details/gift_details?goodsId=' + goodsId + '&show=' + 0 + '&userId=' + fromUser + '&fast=' + fast
    })
  },
  //详情页拉起购物车
  goShopping(e) {
    const that = this;
    let userId = wx.getStorageSync("userId");
    if (userId == '' || userId == null || userId == 'undefined') {
      wx.reLaunch({
        url: '/pages/login/login?path=/pages/list/list'
      })
      return;
    }
    let goodsId = e.currentTarget.dataset.goodsid;
    let goodsType = e.currentTarget.dataset.kinds;
    let fast = e.currentTarget.dataset.fast;

    /*     let userId = wx.getStorageSync("userId");
        if (userId == '' || userId == null || userId == 'undefined') {
          wx.navigateTo({
            url: '/pages/login/login',
          })
          return;
        } */

    wx.navigateTo({
      url: '/pages/gift_details/gift_details?goodsId=' + goodsId + '&show=' + 1 + '&userId=' + fromUser + '&fast=' + fast
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    that.setData({
      pageNum: 1,
      loadingPrompt: false,
      notPrompt: false
    })
    // let userId = wx.getStorageSync("userId");
    // if (userId == '' || userId == null || userId == 'undefined') {
    //   wx.reLaunch({
    //     url: '/pages/login/login?path=/pages/goods/goods'
    //   })
    //   return;
    // }
    that.brandSwitch();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  brandSwitch: function () {
    let that = this;
    request._get(getApp().Api_url + 'brand/which',{}, function (res) {
      if (res.data.code == 200) {
        let entry = res.data.data;
        console.log("品牌数据",entry[0].kinds)
        that.setData({
          nowSwitch:entry[0].kinds
        })
        if(entry.length>1){
          that.setData({
            brandShow:false,
            cateShow:false,
            searchWidth:500
          })
        }else{
          that.setData({
            brandShow:false,
            cateShow:false,
            searchWidth:620
          })
        }
        for (let i = 0; i < entry.length; i++) {
          if (entry[i].kinds == 0) {
            that.setData({
              brandName:entry[i].id,
              brandShow:true
            })
          }else if(entry[i].kinds ==1){
            that.setData({
              cateName:entry[i].id,
              cateShow:true
            })
          }
        }
        if (that.data.nowSwitch==0) {
          that.pullBrandData();
        } else if(that.data.nowSwitch==1){
          that.pullCateData();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this;

    wx.stopPullDownRefresh();
    that.onShow();
    that.showLoading();
    that.setData({
      indexId: 0
    })
  },
  //加载loading弹出层
  showLoading: function () {
    var that = this;
    var timer = null;
    that.setData({
      toastModalStatus: true,
    });
    clearInterval(timer);
    timer = setTimeout(function () {
      that.setData({
        toastModalStatus: false,
      });
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this;
    that.setData({
      pageNum: that.data.pageNum + 1,
      loadingPrompt: true
    })
    //商品列表
      if(that.data.iswitch==1){
        request._get(getApp().Api_url + 'goods/listByCateId', { //商品列表接口
          cateId: that.data.cateId,
          userId: userId,
          pageNum: that.data.pageNum,
          pageSize: 6
        }, function (res) {
          if (res.data.errMsg == 'OK') {
            let newGoodsList = res.data.data;

            if (!newGoodsList || newGoodsList.length <= 0 || newGoodsList == null) {
              that.setData({
                notPrompt: true,
                loadingPrompt: false
              })
            } else {
              let goodsList = that.data.goodsList;
              that.setData({
                goodsList: goodsList.concat(newGoodsList),
                loadingPrompt: false
              })
            }

          }
        }, function () {})
      }
  },
  pullCateData:function(){
    let that = this;
    let userId = wx.getStorageSync("userId");
    request._get(getApp().Api_url + 'brand/cate', { //商品列表接口
      userId: userId
    }, function (res) {
      if (res.data.code == 200) {
        let cateDataArray = res.data.data;
        that.setData({
          categoryArray:cateDataArray
        })
        if(cateDataArray!=null && cateDataArray.length>0){
          let cateItem = cateDataArray[0];
          if(cateItem!=null && cateItem.childs !=null){
            let cateItemList = cateItem.childs;
            that.setData({
              cateItemList:cateItemList
            });
          }
        }
      }
    }, function () {
    })
  },
  pullBrandData:function(){
    let userId = wx.getStorageSync("userId");
    let that = this
    request._get(getApp().Api_url + 'goods/allCategory', { //商品类别接口
      pageNum: 1,
      pageSize: 100,
      userId: userId
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        var navList = res.data.data;
        that.setData({
          navList: navList
        })

        //商品列表
        request._get(getApp().Api_url + 'goods/listByCateId', { //商品列表接口
          cateId: navList[that.data.indexId].cateId,
          userId: userId,
          pageNum: 1,
          pageSize: 6
        }, function (res) {
          if (res.data.errMsg == 'OK') {
            var goodsList = res.data.data;
            var list_goods_img = res.data.oss[0];
            if (list_goods_img) {
              that.setData({
                goods_logo: true
              })
            } else {
              that.setData({
                goods_logo: false
              })
            }
            that.setData({
              goodsList: goodsList,
              list_goods_img: list_goods_img,
              cateId: navList[that.data.indexId].cateId

            })

          }
        })
      }
    })
  }
})